﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FilePathMappingEditor : Form,IComponentEditor
    {
        private object _ActivityNode;
        private FilePathMappingComponent _PathMappingComponent;

        /// <summary>
        /// 
        /// </summary>
        public FilePathMappingEditor()
        {
            InitializeComponent();
        }

        #region Implementation of IComponentEditor

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get { return typeof (FilePathMappingComponent); }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            try
            {
                this._ActivityNode = activityNode;
                this._PathMappingComponent = (FilePathMappingComponent)wfComponent;

                this.txt_FolderPath.Text = this._PathMappingComponent.FolderPath;
                this.txt_Filter.Text = this._PathMappingComponent.FileNameSearchPattern;
                this.ck_IncludeSubFolder.Checked = this._PathMappingComponent.IncludeSubFolders;

                this.txt_ConnStr.Text = this._PathMappingComponent.ConnStr;
                this.cbo_TgtTables.Items.Clear();
                if (!string.IsNullOrEmpty(this.txt_ConnStr.Text))
                {
                    string[] tblNames = DbUtil.GetTableNames(
                        DataProviderType.MSSQL, string.Empty, this.txt_ConnStr.Text);
                    foreach (string tblName in tblNames)
                    {
                        this.cbo_TgtTables.Items.Add(tblName);
                    }

                    this.SelectFieldInComboBox(this.cbo_TgtTables, this._PathMappingComponent.TblName);
                }

                this.SelectFieldInComboBox(this.cbo_TgtIDField, this._PathMappingComponent.IDField);
                this.SelectFieldInComboBox(this.cbo_FilePathField, this._PathMappingComponent.FilePathField);

                this.txt_Pattern.Text = this._PathMappingComponent.FileNameMatchPattern;
                this.num_CatchIndex.Value = this._PathMappingComponent.CatchIndex;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            this._PathMappingComponent.FolderPath = this.txt_FolderPath.Text;
            this._PathMappingComponent.FileNameSearchPattern = this.txt_Filter.Text;
            this._PathMappingComponent.IncludeSubFolders = this.ck_IncludeSubFolder.Checked;

            this._PathMappingComponent.ConnStr = this.txt_ConnStr.Text;
            this._PathMappingComponent.TblName = this.cbo_TgtTables.Text;
            this._PathMappingComponent.IDField = this.cbo_TgtIDField.Text;
            this._PathMappingComponent.FilePathField = this.cbo_FilePathField.Text;

            this._PathMappingComponent.FileNameMatchPattern = this.txt_Pattern.Text;
            this._PathMappingComponent.CatchIndex = (int) this.num_CatchIndex.Value;
        }

        /// <summary></summary>
        public TestResult Test()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        private void SelectFieldInComboBox(ComboBox comboBox, string item)
        {
            if (comboBox.Items.Count > 0 && !string.IsNullOrEmpty(item))
            {
                for (int i = 0; i < comboBox.Items.Count; i++)
                {
                    if (comboBox.Items[i].ToString() == item)
                    {
                        comboBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void btn_RefreshTables_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txt_ConnStr.Text))
            {
                string[] tblNames = DbUtil.GetTableNames(
                    DataProviderType.MSSQL, string.Empty, this.txt_ConnStr.Text);
                if (tblNames != null && tblNames.Length > 0)
                {
                    foreach (string tblName in tblNames)
                    {
                        this.cbo_TgtTables.Items.Add(tblName);
                    }

                    this.SelectFieldInComboBox(this.cbo_TgtTables, this._PathMappingComponent.TblName);
                }
            }

            this.SelectFieldInComboBox(this.cbo_TgtIDField, this._PathMappingComponent.IDField);
            this.SelectFieldInComboBox(this.cbo_FilePathField, this._PathMappingComponent.FilePathField);

        }

        private void btn_Browse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg=new FolderBrowserDialog();
            if(dlg.ShowDialog()==DialogResult.OK)
                this.txt_FolderPath.Text = dlg.SelectedPath;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._PathMappingComponent);
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbo_TgtTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_TgtTables.SelectedIndex>=0)
            {
                string connStr = this.txt_ConnStr.Text;
                string tblName = this.cbo_TgtTables.SelectedItem.ToString();
                this.cbo_TgtIDField.Items.Clear();
                this.cbo_FilePathField.Items.Clear();
                TableColumn[] cols = DbUtil.GetTableColumns(
                    DataProviderType.MSSQL, connStr, string.Empty, tblName);
                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        this.cbo_TgtIDField.Items.Add(col.ColumnName);
                        this.cbo_FilePathField.Items.Add(col.ColumnName);
                    }
                }
            }
        }
    }
}
